package com.glory.dmegcpda.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.glory.dmegcpda.R
import com.glory.dmegcpda.databinding.ActivityKeepTaskListBinding
import com.glory.dmegcpda.utils.PopWindowsUtils
import com.glory.dmegcpda.viewmodel.KeepTaskListViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import kotlinx.android.synthetic.main.activity_keep_task_list.IvEqpType
import kotlinx.android.synthetic.main.activity_keep_task_list.checkEmptyView
import kotlinx.android.synthetic.main.activity_keep_task_list.etEqp
import kotlinx.android.synthetic.main.activity_keep_task_list.etFormName
import kotlinx.android.synthetic.main.activity_keep_task_list.etTime
import kotlinx.android.synthetic.main.activity_keep_task_list.flEqpType
import kotlinx.android.synthetic.main.activity_keep_task_list.rvCheckList
import kotlinx.android.synthetic.main.activity_keep_task_list.tvEqpType
import kotlinx.android.synthetic.main.activity_title.*

/**
 * FileName: KeepTaskListActivity
 * Author: XJH
 * Date: 2021/10/21 17:25
 * Description:
 * History:
 */
open class KeepTaskListActivity : BaseActivity<KeepTaskListViewModel>(),
    View.OnClickListener {

    private var eqpTypePop: PopWindowsUtils.GeneralPop? = null
    private var mBinging : ActivityKeepTaskListBinding? = null

    override fun layoutId(): Int {
        return R.layout.activity_keep_task_list
    }

    private fun setRightListener(title: String, listener: View.OnClickListener?) {
        if (title.isNotEmpty()) {
            tvRight.visibility = View.VISIBLE
            tvRight.text = title
            tvRight.setOnClickListener(listener)
        } else {
            tvRight.visibility = View.GONE
        }
    }

    private fun getText(editText: EditText) : String{
        return editText.text.toString().trim()
    }

    private fun getText(textView: TextView) : String{
        return textView.text.toString().trim()
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinging = DataBindingUtil.setContentView(this,R.layout.activity_keep_task_list)
        mBinging!!.keepTaskListViewModel = viewModel
        rvCheckList.setEmptyView(checkEmptyView)
        tvTitle.text = "保养任务清单"
        setRightListener("查询"){
            if (getText(tvEqpType).isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("请选择设备类型",false)
                return@setRightListener
            }
            viewModel.searchCheckList(getText(etEqp),getText(tvEqpType),getText(etFormName),getText(etTime))
        }

        flEqpType.setOnClickListener(this)
        ivBack.setOnClickListener { finish() }
    }

    override fun initData() {
        viewModel.defUI.startActivity.observe(this) {
            val intent = Intent(this, KeepInfoActivity::class.java)
            intent.putExtras(it)
            startActivityForResult(intent, 101)

        }

        viewModel.defUI.callObserve.observe(this){
            viewModel.searchCheckList(getText(etEqp),getText(tvEqpType),getText(etFormName),getText(etTime))
        }
        viewModel.getEqpType()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == RESULT_OK){
            viewModel.searchCheckList(getText(etEqp),getText(tvEqpType),getText(etFormName),getText(etTime))
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.flEqpType->{
                if (eqpTypePop == null) {
                    eqpTypePop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flEqpType.width,
                        viewModel.eqpType,
                        IvEqpType,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvEqpType.text = codeS
                            }

                        })

                }

                if (eqpTypePop!!.isShowing) {
                    eqpTypePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(IvEqpType)
                    eqpTypePop?.showAsDropDown(flEqpType, 0, 0)
                }
            }
        }
    }
}